<?php
session_start();
require_once ('../lib/config.php');
require_once ('../lib/facebook.php');
require_once ('../lib/friends.php');
require_once ('../lib/friend.php');
require_once ('../lib/test.php');
require_once ('../../simpletest/autorun.php');
require_once ('../lib/init.php');

class TestFunc_getMyCommentOnLink extends UnitTestCase {
    
    function setUp(){
        global $login_button,$facebook;

        
        if(isLoggedIn()){
            try{
//                $me = getOneself();
            }catch(Exception $e){
                print($login_button);
                exit();
            }
        }
    }
    
    //refer to http://www.simpletest.org/en/unit_test_documentation.html
//    function test_getMyCommentOnLink(){ 
//        $data = getMyCommentOnLink();
//        //not null
//        $this->assertNotNull($data);
//        $this->assertTrue(is_array($data));
//    }
    
    function test_getMyCommentOnLink_TEST(){ 

//        $query = "SELECT owner FROM link WHERE link_id IN (
//            SELECT object_id FROM comment 
//            WHERE fromid = me() AND object_id IN (
//                SELECT link_id FROM link WHERE owner IN (
//                    SELECT uid2 FROM friend WHERE uid1 = me()
//                ) AND created_time > ".QUERY_SINCE."
//            ) AND time >".QUERY_SINCE."
//        )";

//        error_log("Fetching Friends");
//        $data = getFriends();
//        var_export($data);
        
////Create friends list
//$friendString = "";
//$first = true;
//foreach($data as $id => $friend){
//    if(!$first){
//        $friendString .= ',';
//    }
//    $friendString .= '"'.$id.'"';
//    $first = false;
//}


        error_log("2: subquery");
        $query = "SELECT link_id FROM link WHERE owner IN (
                    SELECT uid2 FROM friend WHERE uid1 = me()
                ) AND created_time > ".strtotime("-24 month");
//        $data = callFQL($query); 
		/*
         * Result
         */
        // -1 month: 7, 7, 8, 7
        // -6 month : 8, 8, 7
        // -12 month : 12, 8, 11, 8, 15
        // -24: 8
//        var_export($data);
        
//        error_log("2.1: manual");
//        $query = "SELECT link_id FROM link WHERE owner IN (
//                    ".$friendString."
//                ) AND created_time > ".strtotime("-24 month");
//        $data = callFQL($query);
        /*
         * Result
         */
        // -1month:7, 7, 8 
        // -6 month:7, 10, 8 
        // -12 month : 8, 8, 7, 8, 11
        // -24 : 9
//        var_export($data);
        
        error_log("2.2: Multiquery");
        $query1 = "SELECT uid2 FROM friend WHERE uid1 = me()";
        $query2 = "SELECT link_id FROM link WHERE owner IN (
                    SELECT uid2 FROM #query1
                ) AND created_time > ".strtotime("-24 month");
        
        $queries['query1'] = $query1;
        $queries['query2'] = $query2;
        
//        $data = callMultiFQL($queries); 
//        var_export($data);
        /*
         * Result
         */
        //-1 month:8, 12, 7, 8 
        // -6 month: 8, 8, 8 
        // -12 month: 8, 7, 7, 8, 8
        //-24 : 9
 
        
        error_log("3: limit");
        $query = "SELECT object_id FROM comment 
            WHERE fromid = me() AND object_id IN (
                SELECT link_id FROM link WHERE owner IN (
                    SELECT uid2 FROM friend WHERE uid1 = me()  
                ) AND created_time > ".strtotime("-6 month")."
            ) AND time >".strtotime("-6 month");
        
       
//        $data = callFQL($query);
//        var_export($data);
        /*
         * RESULT
         * Without LIMIT : 18, 23, 18
         * LIMIT 10 : 2
         * LIMIT 20 : 4, 4
         * LIMIT 30 : 9, 10, 10
         * LIMIT 40 : 12, 10, 13
         * LIMIT 50 : 11, 11, 11
         * LIMIT 60 : 15, 15, 20, 15
         * LIMIT 70 : 18, 20, 18
         * LIMIT 80 : 18, 18, 18
         */

        
        error_log("original");
        $query = "SELECT owner FROM link WHERE link_id IN (
            SELECT object_id FROM comment 
            WHERE fromid = me() AND object_id IN (
                SELECT link_id FROM link WHERE owner IN (
                    SELECT uid2 FROM friend WHERE uid1 = me()
                ) AND created_time > ".strtotime("-1 day")."
            ) AND time >".strtotime("-1 day")."
        )";
        
        for($i = 0; $i < 3; $i++){
            $data = callFQL($query);
        }
        var_export($data);
        
        /*
         * Result
         * Without LIMIT : 19, 23, 19, 21
         * LIMIT 10 : 3,2,2
         * LIMIT 20 : 4,4,4
         * LIMIT 30 : 11, 12, 18
         * LIMIT 40 : 11, 13, 11
         * LIMIT 50 : 16,12,12
         * LIMIT 60 : 15,21,25
         * LIMIT 70 : 18,18,31
         * LIMIT 80 : 32,32,18
         * 
         * -1 day : 12,8,11
         * -1 week : 9,13,7
         * -1 month : 9,10,9
         * -6 month : 22,17,18
         * -12 month : 26,26,25
         * -24 month : 31,25,25
         */
        
        //not null
        $this->assertNotNull($data);
        $this->assertTrue(is_array($data));
    }
    
}
?>
